package cz.matasek.dbLogger;

import cz.matasek.dbLogger.gui.Dialog;
import java.io.File;
import java.util.HashMap;
import java.util.Map;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import org.w3c.dom.Document;

/**
 * The application settings class. The settings are stored in the
 * <code>settings.xml</code> file (user can change the settings manually).
 *
 * @author Luboš Matásek
 */
public class Settings {

    /**
     * Settings keys. Uppercase version of the element names in the settings XML
     * document.
     */
    public enum Key {

        /**
         * Name of the function, which returns prefix for all logging objects.
         */
        PREFIX_FUNCTION_NAME,
        /**
         * Default prefix for the logging objects.
         */
        DEFAULT_LOGGING_PREFIX,
        /**
         * Suffix of the logging table.
         */
        LOGGING_TABLE_SUFFIX,
        /**
         * Suffix of the history table.
         */
        HISTORY_TABLE_SUFFIX,
        /**
         * Suffix of the sequence.
         */
        SEQUENCE_SUFFIX,
        /**
         * Suffix of the logging trigger.
         */
        LOGGING_TRIGGER_SUFFIX,
        /**
         * Suffix of the history trigger.
         */
        HISTORY_TRIGGER_SUFFIX,
        /**
         * Name of the directory containing SQL scripts.
         */
        SQL_DIRECTORY,
        /**
         * Name of the directory containing SQL scripts.
         */
        XSL_DIRECTORY,
        /**
         * XSLT transformation for exporting history view into HTML.
         */
        XSL_HISTORY,
        /**
         * XSLT transformation for exporting logging table query into HTML.
         */
        XSL_QUERY;

        /**
         * Returns string representation of the logging availability.
         *
         * @return logging availability
         */
        @Override
        public String toString() {
            return name().toLowerCase();
        }
    }
    /**
     * The file, settings are stored in.
     */
    private final File SETTINGS_FILE = new File("settings.xml");
    /**
     * Mapping of the settings keys to values.
     */
    private Map<Key, String> settingsValues;
    /**
     * Static instance of the <code>Settings</code> class.
     */
    private static Settings instance;

    /**
     * Creates a new <code>Settings</code> object.
     */
    private Settings() {
        settingsValues = new HashMap<Key, String>();

        try {
            // parses the XML
            DocumentBuilder docBuilder =
                    DocumentBuilderFactory.newInstance().newDocumentBuilder();
            Document doc = docBuilder.parse(SETTINGS_FILE);

            // reads settings
            for (Key key : Key.values()) {
                settingsValues.put(key,
                        doc.getElementsByTagName(key.toString()).item(0).getTextContent().trim());
            }
        } catch (Exception ex) {
            Dialog.error(ex.getMessage());
        }
    }

    /**
     * Returns settings value for the specified key.
     *
     * @param key settings key
     * @return settings value
     */
    public static String get(Key key) {
        if (instance == null) {
            instance = new Settings();
        }

        return instance.settingsValues.get(key);
    }
}
